<html>
<head>
    <title>字符串内置函数</title>
</head>
<body>

<#-- capitalize 字符串中所有单词的首字母大写 -->
${"  green  mouse"?capitalize}
${"GreEN mouse"?capitalize}

<#-- boolean 字符串转为布尔值 -->
<#assign booleanVal="true">
<#if (booleanVal?boolean)> True <#else> False </#if>

<#-- cap_first 字符串中的首单词的首字母大写 -->
${"  green mouse"?cap_first}
${"GreEN mouse"?cap_first}
${"- green mouse"?cap_first}

<#-- xml 和 xhtml -->
${"<?xml version=\"1.0\" encoding='UTF-8'?><note><to>George</to></note>"?xml}
${"<?xml version=\"1.0\" encoding='UTF-8'?><note><to>George</to></note>"?xhtml}

<#-- word_list -->
<#assign words = "   a bcd, .   1-2-3"?word_list>
<#list words as word>[${word}]</#list>

<#-- url_path -->
${"http://localhost:8090/test/index.html"?url_path("UTF-8")}
${"D:\\learn\\open_source\\freemarker_learn\\pom.xml"?url_path("UTF-8")}

<#-- upper_case -->
${"GrEeN MoUsE"?upper_case}

<#-- uncap_first 所有单词首字母小写 -->
${"Hello World"?uncap_first}

<#-- chop_linebreak -->
<#assign str1="Java template engine;\n">
<#assign str2="generates HTML web pages, e-mails, configuration files, source code, etc.">
|${str1} ${str2}|
|${str1?chop_linebreak} ${str2}|

<#-- ensure_ends_with -->
${"foo"?ensure_ends_with("/")}
${"foo/"?ensure_ends_with("/")}

<#-- ensure_starts_with -->
${ "foo"?ensure_starts_with("/") }
${ "/foo"?ensure_starts_with("/") }
${ "https://www.hxstrive.com"?ensure_starts_with("[a-zA-Z]+://", "http://") }
${ "www.hxstrive.com"?ensure_starts_with("[a-zA-Z]+://", "http://") }
${ "HTTP://www.hxstrive.com"?ensure_starts_with("http://", "http://www", "i") }
${ "www.hxstrive.com"?ensure_starts_with("http://", "http://", "i") }


<#-- The date_format, time_format and datetime_format settings must match this format! -->
<#assign someDate = "Oct 25, 1995"?date>
<#assign someTime = "3:05:30 PM"?time>
<#assign someDatetime = "Oct 25, 1995 03:05:00 PM"?datetime>

<#-- Changing the setting value changes the expected format: -->
<#setting datetime_format="iso">
<#assign someDatetime = "1995-10-25T15:05"?datetime>

<#-- ends_with -->
<#if "ahead"?ends_with("head")>
    字符串以head结尾
<#else>
    不是以head结尾
</#if>
<#if "head"?ends_with("head")>
    字符串以head结尾
<#else>
    不是以head结尾
</#if>

<#-- html -->
<#assign htmlVal="<label>Username: Administrator</label>">
<#assign htmlAttr="<i>color:red;</i>">
<p attr="${htmlAttr?html}">${htmlVal?html}</p>


<#-- index_of -->
${"abcabc"?index_of("bc")}
${"abcabc"?index_of("bc", 2) }

<#-- j_string -->
<#assign beanName = 'The "foo" bean.'>
String BEAN_NAME = "${beanName?j_string}";

<#-- js_string -->
<#assign user = "Big Joe's \"right hand\"">
<script>
    alert("Welcome ${user?js_string}!");
</script>

<#-- json_string -->
<#assign jsonStr="Administrator,</'admin']]>">
{"name":"${jsonStr?json_string}", "age":28}


<#-- keep_after -->
正常：${"abcdefgh"?keep_after("de")}
没有找到：${"abcdefgh"?keep_after("hah")}
空参数：${"abcdefgh"?keep_after("")}
标志位参数：${"foo : bar"?keep_after(r"\s*:\s*", "r")}

<#-- keep_before -->
正常：${"abcdef"?keep_before("de")}
没有找到：${"abcdef"?keep_before("hah")}
空参数：${"abcdef"?keep_before("")}
标志位参数：${"foo : bar"?keep_before(r"\s*:\s*", "r")}

<#-- last_index_of -->
${"abcabc"?last_index_of("ab")}

<#-- left_pad -->
[${""?left_pad(5)}]
[${"a"?left_pad(5)}]
[${"ab"?left_pad(5)}]
[${"abc"?left_pad(5)}]
[${"abcd"?left_pad(5)}]
[${"abcde"?left_pad(5)}]
[${"abcdef"?left_pad(5)}]
[${"abcdefg"?left_pad(5)}]
[${"abcdefgh"?left_pad(5)}]
[${""?left_pad(5, "-")}]
[${"a"?left_pad(5, "-")}]
[${"ab"?left_pad(5, "-")}]
[${"abc"?left_pad(5, "-")}]
[${"abcd"?left_pad(5, "-")}]
[${"abcde"?left_pad(5, "-")}]
[${""?left_pad(8, ".oO")}]
[${"a"?left_pad(8, ".oO")}]
[${"ab"?left_pad(8, ".oO")}]
[${"abc"?left_pad(8, ".oO")}]
[${"abcd"?left_pad(8, ".oO")}]


<#-- length -->
length=${"administrator"?length}

<#-- matches -->
<#if "fxo"?matches("f.?o")>
    Matches.
<#else>
    Does not match.
</#if>

<#assign res = "foo bar fyo"?matches("f.?o")>
<#if res>
    Matches.
<#else>
    Does not match.
</#if>

Matching sub-strings:
<#list res as m>
    - ${m}
</#list>

<#-- Entire input match -->
<#assign res = "John Doe"?matches(r"(\w+) (\w+)")>
<#if res> <#-- Must not try to access groups if there was no match! -->
    First name: ${res?groups[1]}
    Second name: ${res?groups[2]}
</#if>

<#-- Subtring matches -->
<#assign res = "aa/rx; ab/r;"?matches("(.+?)/*(.+?);")>
<#list res as m>
    - "${m}" is "${m?groups[1]}" per "${m?groups[2]}"
</#list>


</body>
</html>